package com.spring.boot.demo.common.resp;

import java.io.Serializable;

/**
 * @author ZhuYouBin
 * @version 1.0.0
 * @Date: 2022/10/29 21:16
 * @Description 统一响应结果类
 */
public class Result<T> implements Serializable {
    /** 状态码 */
    private Integer statusCode;
    /** 提示信息 */
    private String message;
    /** 响应数据 */
    private T data;

    /** 私有构造方法 */
    private Result() {}

    /************************** common method **************************/
    /* 响应成功 */
    public static <T> Result<T> success() {
        Result<T> result = new Result<>();
        result.setStatusCode(ResultEnum.SUCCESS.getStatusCode());
        result.setMessage(ResultEnum.SUCCESS.getMessage());
        return result;
    }

    /* 响应失败 */
    public static <Void> Result<Void> failure() {
        Result<Void> result = new Result<>();
        result.setStatusCode(ResultEnum.FAILED.getStatusCode());
        result.setMessage(ResultEnum.FAILED.getMessage());
        return result;
    }

    /* 响应失败 */
    public static <Void> Result<Void> failure(String message) {
        Result<Void> result = new Result<>();
        result.setStatusCode(ResultEnum.FAILED.getStatusCode());
        result.setMessage(message);
        return result;
    }

    /* 响应失败 */
    public static <Void> Result<Void> failure(int statusCode, String message) {
        Result<Void> result = new Result<>();
        result.setStatusCode(statusCode);
        result.setMessage(message);
        return result;
    }

    /* 响应失败 */
    public static <Void> Result<Void> failure(ResultEnum resultEnum) {
        Result<Void> result = new Result<>();
        result.setStatusCode(resultEnum.getStatusCode());
        result.setMessage(resultEnum.getMessage());
        return result;
    }

    /* 自定义响应状态码 */
    public Result<T> statusCode(int statusCode) {
        this.setStatusCode(statusCode);
        return this;
    }

    /* 自定义响应提示信息 */
    public Result<T> message(String message) {
        this.setMessage(message);
        return this;
    }

    /* 自定义响应数据 */
    public Result<T> data(T data) {
        this.setData(data);
        return this;
    }

    /************************** getter and setter **************************/
    public Integer getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(Integer statusCode) {
        this.statusCode = statusCode;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "Result{" +
                "statusCode=" + statusCode +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }
}
